<!DOCTYPE html>
<html>
<head>
<include file="Include:meta" />
<title>添加<if condition="$sw eq 1">商务跟进</if>项目-目的地运营集团数据管理平台</title>
<include file="Include:style" />
</head>
<body>
<include file="Include:header" />
<include file="Include:left" />
<div id="main">
	<div class="crumbs">
		<div class="crumbs-text"><if condition="$sw eq 1">商务跟进</if>项目 > 添加<if condition="$sw eq 1">商务跟进</if>项目</div>
	</div>
	<div id="content">
		<form action="__APP__/Project/project_add" method="post" id="project_add_form">
			<input type="hidden" name="action" value="add">
			<input type="hidden" name="sw" value="{$sw}">
			<div class="form-main">
				<div class="form-title">添加<if condition="$sw eq 1">商务跟进</if>项目</div>
				<div class="form-item">
					<div class="input-label">所属中心</div>
					<div class="input-item">
						{:get_center_name($center_id)}
						<input type="hidden" name="center_id" class="project_type" value="{$center_id}">
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>项目编号</div>
					<div class="input-item">
						<input type="text" name="project_num1" value="{$project_type_number}" class="form-control mr5" style="width:40px;" readonly autocomplete="off">
						<input type="text" name="project_num2" value="{$project_max_number}" maxval="{$project_max_number}" class="form-control" style="width:46px;" maxlength="4" autocomplete="off">
						<span class='error-item'>必填项</span>
						<span class='error-item'>必须是4位数字</span>
						<span class='error-item'>不能大于<span class="project_max_number">{$project_max_number}</span></span>
						<span class='error-item'>不能为0000</span>
						-
						<input type="text" name="project_num3" class="form-control" style="width:36px;" maxlength="2" autocomplete="off">
						<span class='error-item'>必须是2位字符</span>
					</div>
				</div>
				<if condition="$sw neq 1">
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>项目名称</div>
					<div class="input-item" style="position:relative;">
						<input type="text" name="project_name" class="form-control required-text w500 project_add_inp" autocomplete="off" placeholder="省+市+项目简称+方案名称">
						<span class="error-item">必填项</span>
						<div class="project_add_search">
							<ul></ul>
						</div>
					</div>
				</div>
				</if>
				<if condition="$sw eq 1">
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>关联市场信息</div>
						<div class="input-item">
							<select name="info_id" class="form-control required-selted" autocomplete="off">
								<option value="-10" selected>请选择</option>
								<foreach name="info" item="vo">
									<option value="{$vo['id']}"><if condition="$vo['jc_id'] neq NULL">[JC]</if>[{$vo['customer_num']}]{$vo['customer_name']}</option>
								</foreach>
							</select>
							<span class="error-item">必填项</span>
						</div>
					</div>
				</if>
				<if condition="($sw neq 1) and ($center_id eq 130005)">
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>所属部门</div>
					<div class="input-item">
						<select name="dept_id" class="form-control required-selted" autocomplete="off">
							<option value="-10" selected>请选择</option>
							<foreach name="dept_list" item="vo">
								<option value="{$vo['id']}">{$vo['first_name']}</option>
							</foreach>
						</select>
						<span class="error-item">必填项</span>
					</div>
				</div>
				</if>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>项目关键字</div>
					<div class="input-item">
						<input type="text" name="key_word" class="form-control required-text w200" autocomplete="off" placeholder="最多输入6个字" maxlength="6">
						<span class="error-item">必填项</span>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span><if condition="$center_id eq 130005">项目负责人<else/>项目经理</if></div>
					<div class="input-item">
						<select name="manager_uid" class="form-control required-selted" autocomplete="off">
							<option value="-10" selected>请选择</option>
							<foreach name="user_list" item="vo">
								<option value="{$vo['id']}">{$vo['username']}</option>
							</foreach>
						</select>
						<span class="error-item">必填项</span>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span><if condition="$center_id eq 130005">项目群负责人<else/>项目总监</if></div>
					<div class="input-item">
						<select name="director_uid" class="form-control required-selted" autocomplete="off">
							<option value="-10" selected>请选择</option>
							<foreach name="user_list" item="vo">
								<option value="{$vo['id']}">{$vo['username']}</option>
							</foreach>
						</select>
						<span class="error-item">必填项</span>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>启动日期</div>
					<div class="input-item">
						<input type="text" name="start_date" class="form-control Wdate w100 required-text" onclick="WdatePicker()" readonly autocomplete="off">
						<if condition="($sw neq 1) and ($center_id eq 130005)">
						至 
						<input type="text" name="end_date" class="form-control Wdate w100 required-text" onclick="WdatePicker({isShowClear:true})" readonly autocomplete="off">
						<span class="error-item">必填项</span>
						<span class="input-prompt"><i class="glyphicon glyphicon-exclamation-sign ml10"></i> 结束日期</span>
						</if>
					</div>
				</div>
				<!--<if condition="(get_permission('XM',NULL,2)) or (get_permission('XM',NULL,1)) or (get_permission('SW',NULL,2)) or (get_permission('SW',NULL,1))">
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>添加人</div>
					<div class="input-item">
						<select name="add_uid" class="form-control required-selted" autocomplete="off">
							<option value="-10" selected>请选择</option>
							<foreach name="user_list" item="vo">
								<option value="{$vo['id']}">{$vo['username']}</option>
							</foreach>
						</select>
						<span class="error-item">必填项</span>
					</div>
				</div>
				</if>-->
			</div>
			<div class="form-btn-item"><a class="btn btn-submit" href="javascript:;">提交</a><button class="btn btn-default goback" type="button">返回</button></div>
		</form>
	</div>
</div>
<!--弹框-->
<div class="modal fade" id="url-modal">
	<div class="modal-dialog w600">
		<div class="modal-content">
			<div class="modal-header">
				<h4>提交</h4>
				<i class="fa fa-times" aria-hidden="true" data-dismiss="modal"></i>
			</div>
			<div class="modal-body clearfix" style="font-size:14px;">
				提交成功
			</div>
			<div class="modal-footer">
				
			</div>
		</div>
	</div>
</div>
<include file="Include:javascript" />
<script type="text/javascript">
$(function() {
	$("[name='project_name']").focus(function(){
		$('.project_add_search').show();
	})
	$("[name='project_name']").blur(function(){
		$('.project_add_search').hide();
	})
	$("[name='project_name']").bind('propertychange input', function () {
		var val = $(this).val(),
			center_id = $("[name='center_id']").val();
		if(val!=''){
			$(this).addClass('active');
			$('.project_add_search ul').show()
			$.ajax({
				type:"POST",
				asnyc:false,
				dataType:"json",
				data:{"val":val,"center_id":center_id},
				url:"__APP__/Project/project_name",
				success:function(data){
					if(data){
						var html='';
						for(var i=0;i<data.length;i++){
							html+= "<li>"+data[i].project_name+"</li>"
						}
						$('.project_add_search ul').html(html);
					}else{
						$('.project_add_search ul').html("<li>没有匹配项目</li>");
					}
				}
			});
		}else{
			$(this).removeClass('active');
			$('.project_add_search ul').hide().html("");
		}
	})
	//项目分类联动项目编号
	$("[name='type_id']").on("change",function(){
		var val = $(this).val(),
			num = $("[name='type_id'] option:selected").attr("data-id");
		if(val==-10){
			$("[name='project_num1'],[name='project_num2'],[name='project_num3']").val("");
			$("[name='project_num1']").removeAttr("maxval");
			$(".project_max_number").text("");
		}else{
			$.ajax({
				type:"POST",
				asnyc:false,
				dataType:"json",
				data:{"project_type_number":num},
				url:"__APP__/Project/project_add_max_number",
				success:function(data){
					$("[name='project_num1']").val(num);
					$("[name='project_num2']").val(data).attr("maxval",data);
					$(".project_max_number").text(data);
					$("[name='project_num3']").val("");

				}
			});
		}
		$("[name='project_num1']").find(".error-item").hide();
	});

	//表单验证
	$(".btn-submit").click(function(e){
		var tag = true,
			center_id = "{$center_id}",
			sw = "{$sw}";
		$(".required-text").each(function(){
			var val = $(this).val();
			if(val=="" || /^\s+$/.test(val)){
				$(this).nextAll(".error-item").show();
				tag = false;
			}
		});
		$(".required-selted").each(function(){
			if($(this).val()==-10){
				$(this).nextAll(".error-item").eq(0).show();
				tag = false;
			}
		});
		//项目编号
		$("[name='project_num2']").each(function(){
			var $val = $(this).val(),
				$max = $(this).attr("maxval");
			
			if($val==""){
				$(this).next(".error-item").show();
				tag = false;
			}else if(!/\d{4}/.test($val)){
				$(this).nextAll(".error-item").eq(1).show();
				tag = false;
			}else if($val-0 > $max-0){
				$(this).nextAll(".error-item").eq(2).show();
				tag = false;
			}
		});
		$("[name='project_num3']").each(function(){
			var $val = $(this).val();
			
			if($val!="" && !/[a-zA-Z0-9]{2}/.test($val)){
				$(this).next(".error-item").show();
				tag = false;
			}
		});
		start = $("[name='start_date']").val();
		if(sw!=1){
			if(center_id==130005){
				end = $("[name='end_date']").val();
				if(end!=''){
					//前后日期判断
					function compareDate(date1,date2){
						let Y1 = parseInt(date1.substr(0, 4)),
							Y2 = parseInt(date2.substr(0, 4)),
							M1 = parseInt(date1.substr(5, 2)),
							M2 = parseInt(date2.substr(5, 2)),
							D1 = parseInt(date1.substr(8, 2)),
							D2 = parseInt(date2.substr(8, 2));
						let a = Y1 < Y2,
							b = Y1 == Y2 && M1 < M2,
							c = Y1 == Y2 && M1 == M2 && D1 < D2;

						if (a || b || c) {
							return true;
						} else {
							return false;
						}
					}
					if(compareDate(end,start)){
						defeated_alert('结束日期不能早于开始日期');
						tag = false;
					}
				}
			}
		}
		
		if(tag){
			$.ajax({
				type:"POST",
				asnyc:false,
				dataType:"json",
				data:$("#project_add_form").serialize(),
				url:"__APP__/Project/project_doadd",
				success:function(data){
					if(data['status']==0){
						defeated_alert(data['msg']);
					}else{
						$("#url-modal").modal();
					}
					if(sw==1){
						$("#url-modal .modal-footer").html("<a class='btn' href='__APP__/Project/business_follow/center_id/"+data['center_id']+"'>返回列表</a><a class='btn' href='__APP__/Project/project_bg/id/"+data['id']+"'>填报项目启动表</a>")
					}else{
						$("#url-modal .modal-footer").html("<a class='btn' href='__APP__/Project/project_list/center_id/"+data['center_id']+"'>返回列表</a><a class='btn' href='__APP__/Project/project_bg/id/"+data['id']+"'>填报项目启动表</a>")
					}
					
				}
			});
		}
	});
	
	//隐藏错误提示
	$(".required-text,[name='project_num2'],[name='project_num3']").focus(function(){
		$(this).nextAll(".error-item").hide();
	});
	$(".required-selted").change(function(){
		$(this).nextAll(".error-item").hide();
	});
	$("[name='type_id']").change(function(){
		$("[name='project_num2']").nextAll(".error-item").hide();
	});
});
</script>
</body>
</html>